package com.itheima.tlias84.servrce.impl;

import com.itheima.tlias84.annotaion.ReportLog;
import com.itheima.tlias84.dto.SelectAllDeptDTO;
import com.itheima.tlias84.entity.Dept;
import com.itheima.tlias84.mapper.DeptMapper;
import com.itheima.tlias84.po.InsertDeptParam;
import com.itheima.tlias84.servrce.DeptService;
import jakarta.websocket.server.PathParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DeptServiceImpl implements DeptService {
    private static final Logger log = LoggerFactory.getLogger(DeptServiceImpl.class);
    @Autowired
    DeptMapper deptMapper;

    @Override
    public List<SelectAllDeptDTO> selectAll() {
        List<SelectAllDeptDTO> dtos = deptMapper.selectAll();
        return dtos;
    }

    @Override
    @ReportLog
    public int insertDept(InsertDeptParam param) {
        Dept dept = new Dept();
        dept.setName(param.getName());
        dept.setUpdateTime(LocalDateTime.now());
        dept.setCreteTime(LocalDateTime.now());
        int flag = deptMapper.insertDept(dept);
        return flag;
    }

    @Override
    public int deleteById(Integer id) {
        return deptMapper.deleteById(id);
    }

    @Override
    public SelectAllDeptDTO selectById(Integer id) {

        SelectAllDeptDTO dto = deptMapper.selectById(id);
        return dto;
    }

    @Override
    public int updateById(Dept dept) {
        //判断名字和id是否重复
        List<SelectAllDeptDTO> dtos = deptMapper.selectAll();

        log.info("dtos:{}", dtos);
        List<String> collect = dtos.stream().map(t -> t.getName()).toList();
        List<Integer> list = dtos.stream().map(t -> t.getId()).toList();
        if ( !list.contains(dept.getId())) {
            log.info("id不存在");
            return 0;
        }
        if (collect.contains(dept.getName())) {
            log.info("名字重复");
            return 0;
        } else {
            dept.setUpdateTime(LocalDateTime.now());
            int i = deptMapper.updateById(dept);
            System.out.println(i);
            return i;
        }
    }

    @Override
    public int batchDelete(List<Integer> ids) {
        log.info("ids:{}", ids);
        return deptMapper.batchDelete(ids);
    }

}
